package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.manger.AutoOpenServerMgr;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/serverRoleType")
public class ServerRoleType extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(ServerRoleType.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("request serverRoleType");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		JSONObject paramObj = httpJsonWebParam.getParamObj();
		int channelId = 0;
		int type = 1 ;
		int level = 2;
		String areaSplitFlow = "";
		try{
			channelId = paramObj.getIntValue("channelId");
			type = paramObj.getIntValue("type");
			level = paramObj.getIntValue("level");
			if(paramObj.containsKey("areaSplitFlow")){
				areaSplitFlow = paramObj.getString("areaSplitFlow");
			}
		}catch (Exception e){
			logger.error("serverRoleType argument error:" + e);
			sendResult(response, 0, "argument error");
			return;
		}

		AutoOpenServerMgr.setAutoOpenConfig(channelId, type, level, areaSplitFlow);

		JSONObject resultObj = new JSONObject();
		resultObj.put("code", 0);
		sendMessage(response, resultObj.toJSONString());
	}
}
